<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>通用盘点分析</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        Ext.define('InventoryCommonReportModel', {
          extend : 'Ext.data.Model',
          fields : [{name: 'id'},
            {
              name: 'vendor_name'
            },{
              name: 'warehouse_name'
            },{
              name: 'product_name'
            },{
              name: 'product_number'
            },{
              name: 'quantity',
              type: 'int'
            },{
              name: 'gl_quantity',
              type: 'int'
            },{
              name: 'unit_price',
              type: 'float'
            },{
              name: 'amount',
              type: 'float'
            },{
              name: 'gl_amount',
              type: 'float'
            }
          ]
        });
        
        //产品
        var productCBB = Ext.create('Ext.form.ComboBox', {
          store: productStore,
          fieldLabel: '产品',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'product_id',
          loadingText: '搜索中...',
          anchor: '100%',
          pageSize: 10,
          minChars: 0,
          listConfig: {
            loadingText: 'Searching...',
            getInnerTpl: function() {
              return '<div style="float:left; width:200px" data-qtip="{name}">{name:ellipsis(18)}</div><div style="float:left; width:110px">{number}</div>{is_suit}';
            }
          }
        });

        //供应商
        var vendor = Ext.create('Ext.form.ComboBox', {
          store: vendorStore,
          fieldLabel: '供应商',
          blankText: "供应商必须选择",
          valueField: 'id',
          displayField: 'name',
          name: 'vendor_id',//如果不想提交displayField，则在这儿指定要提交的Key，value就是valueField．
          emptyText: '请选择...',          
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,
          readOnly:false,
          minChars: 0,
          editable:false
        });

        vendor.on('select', function(combo, record, index) {
          vendorGroup.disable();
        });

        //所有仓库
        
        var warehouse = Ext.create('Ext.form.ComboBox', {
          store: warehouseStore,
          fieldLabel: '选择仓库',
          blankText: "仓库必须选择",
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'warehouse_id',
          loadingText: '搜索中...',
          anchor: '95%',
          //pageSize: 10,
          minChars: 0
        });      

        warehouse.on('select', function(combo, record, index) {
          warehouseGroup.disable();
        });

        //所有盘点日期
        Ext.define('InventoryDateModel', {
          extend : 'Ext.data.Model',
          fields : [{name: 'id'},
            {
              name: 'id'
            }, {
              name: 'action_date',
              type: 'date',
              dateFormat: 'Y/m/d',
              convert:function(value,record){
                return Ext.util.Format.date(value,'Y-m-d');
              }
            }
          ]
        });

        var inventoryDateStore = Ext.create('Ext.data.Store', {
          model : 'InventoryDateModel',
          proxy : {
            type : 'ajax',
            url: "/inventory/getAllInventoryDate",
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          autoLoad : true
        });
        

        var inventoryDate = Ext.create('Ext.form.ComboBox', {
          store: inventoryDateStore,
          fieldLabel: '盘点日期',
          allowBlank: false,
          blankText: "盘点日期必须选择",
          emptyText: '请选择...',
          valueField: 'action_date',
          displayField: 'action_date',
          name: 'action_date',          
          loadingText: '搜索中...',
          anchor: '95%',
          minChars: 0,          
          triggerAction:'all'          
        });   

        var vendorGroup = Ext.create('Ext.form.field.Checkbox', {
          name: 'vendor_group',
          clearCls: 'allow-float',
          itemCls: 'float-left',
          fieldLabel: '供应商分组'
        })

        var warehouseGroup = Ext.create('Ext.form.field.Checkbox', {
          name: 'warehouse_group',
          clearCls: 'allow-float',
          itemCls: 'float-left',
          fieldLabel: '仓库分组'
        })

        var submitButton = Ext.create('Ext.Button', {
          text: '执行统计',
          iconCls: 'finding',
          handler: function(){
            if (queryFormPanel.form.isValid()) {
              statsResultDS.removeAll();
              statsResultDS.getProxy().extraParams.vendor_id = vendor.getValue();
              statsResultDS.getProxy().extraParams.warehouse_id = warehouse.getValue();
              if (vendor.getValue()==""){
                statsResultDS.getProxy().extraParams.vendor_group = vendorGroup.getValue();
              }
              if (warehouse.getValue()==""){
                statsResultDS.getProxy().extraParams.warehouse_group = warehouseGroup.getValue();
              }              
              statsResultDS.getProxy().extraParams.action_date = inventoryDate.getRawValue();
              statsResultDS.getProxy().extraParams.product_id = productCBB.getValue();
              statsResultDS.load({callback: function(record, options, success){
                  if (success == false){
                    alert("您没有权限执行此操作！");
                  }else{
                    amountLabel.setText("总金额:"+Ext.util.Format.number(Ext.JSON.decode(options.response.responseText).amount,'0,000.00'));
                    glAmountLabel.setText("损益总金额:"+Ext.util.Format.number(Ext.JSON.decode(options.response.responseText).gl_amount,'0,000.00'));
                  }
                }
              });
            }
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:70,
          handler: function(){
            queryFormPanel.form.reset();
            vendorGroup.enable();
            warehouseGroup.enable();
          }
        });
        
        var amountLabel = Ext.create('Ext.form.Label', {
          width:130,
          name: 'amount_label'
        })

        var glAmountLabel = Ext.create('Ext.form.Label', {
          width:170,
          name: 'gl_amount_label'
        })

        amountLabel.setText("总金额:0");
        glAmountLabel.setText("损益总金额:0");

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          //baseCls: 'x-plain',
          //renderTo: 'query_form',
          fieldDefaults: {
            labelWidth: 65,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1200},
              title: '统计条件',
              border: true,
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .18,
                      layout: 'anchor',
                      xtype: 'container',
                      labelWidth: 65,
                      items: [vendor]
                    }, {
                      columnWidth: .18,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [warehouse]
                    }, {
                      columnWidth: .4,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [productCBB]
                    }]
                },{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .18,
                      layout: 'anchor',
                      xtype: 'container',
                      labelWidth: 65,
                      items: [vendorGroup]
                    }, {
                      columnWidth: .18,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [warehouseGroup]
                    },{
                      columnWidth: .14,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [inventoryDate]
                    }, {
                      columnWidth: .09,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [submitButton]
                    }, {
                      columnWidth: .1,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [clearButton]
                    }]
                }]
            }]
        });

        //显示销售订单操作时的窗口介面。
        //----------------------------------销售订单Grid-------------------------
        //销售订单Grid Store
        Ext.define('InventoryStatsResultModel', {
          extend : 'Ext.data.Model',
          fields : [{name: 'id'},
            {
              name: 'vendor_name'
            },{
              name: 'warehouse_name'
            },{
              name: 'product_name'
            },{
              name: 'product_number'
            },{
              name: 'quantity',
              type: 'int'
            },{
              name: 'gl_quantity',
              type: 'int'
            },{
              name: 'cost_price',
              type: 'float'
            },{
              name: 'amount',
              type: 'float'
            },{
              name: 'gl_amount',
              type: 'float'
            }
          ]
        });

        var statsResultDS = Ext.create('Ext.data.Store', {
          autoDestroy : true,
          model : 'InventoryStatsResultModel',
          proxy : {
            type : 'ajax',
            url: '/inventory/getInventoryInfoForCommonReport',
            extraParams:{'product_id' : 0},
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          autoLoad : true
        });

        //生成要订单的产品的Grid
        var statsResultGrid = Ext.create('Ext.grid.Panel', {
          title:'统计结果列表',
          store: statsResultDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '供应商',
              dataIndex: 'vendor_name',
              width: 170
            },{
              header: '仓库',
              dataIndex: 'warehouse_name',
              width: 170
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 270
            },{
              header: '编号',dataIndex: 'product_number',width: 100
            },{
              header: '盘点数量',
              dataIndex: 'quantity',
              align:'right',
              width: 75
            },{
              header: '均价',
              dataIndex: 'cost_price',
              align:'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 75
            },{
              header: '金额',
              dataIndex: 'amount',
              align:'right',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              width: 75
            },{
              header: '损益数量',
              dataIndex: 'gl_quantity',
              align:'right',
              width: 75
            },{
              header: '损益金额',
              dataIndex: 'gl_amount',
              renderer: Ext.util.Format.numberRenderer('0,000.00'),
              align:'right',
              width: 75
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: screenWidth-165,
          height: screenHeight-330,
          frame: true,
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
          bbar: [Ext.create('Ext.toolbar.Paging', {
              pageSize: pageSize + 10,
              store: statsResultDS,
              displayInfo: true,
              displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
              emptyMsg: "没有记录"
            }),'->','-',amountLabel,'-',glAmountLabel,'-']
        });    

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-165,
          height: screenHeight-220,
          frame:false,          
          layout: {type: 'table',columns:1},          
          items:[queryFormPanel,statsResultGrid]
        });
        workbenchPanel.render("common_inventory_status_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="common_inventory_status_workbench_panel"></div>
  </body>
</html>